$(document).ready(function(){
    function repeatEachSecond(){
        adjustSides();    
        bannerNextImage();
        setTimeout(function (){
            repeatEachSecond();
        }, 1000);
    }
    /*side adjustor*/
    function adjustSides(){
        var sideWidth = ($(window).width() - $("#topMid").width())/2;
        if (sideWidth <0 ) {
            sideWidth = 0;
        }
        var sideLeft = sideWidth + 1400;
        $("#topLeft").width(sideWidth);
        $("#topRight").width(sideWidth);
        $("#topRight").offset({
            left: sideLeft
        });
        $("#botLeft").width(sideWidth);
        $("#botRight").width(sideWidth);
        $("#botRight").offset({
            left: sideLeft
        });
    }
    /* fadeBanner */
    var canChange = true;
    var next = true;
    var startX = 150;
    var positionX = startX;
    var i = 1;    
    while (next){ 
        if ($("#fadeBanner").attr("data-small"+i) != undefined){
            $("#fadeBanner").append('<img id="small'+ i +'" data-order="'+ i +'" class="pointer banner_smallImages" src="'+ $("#fadeBanner").attr("data-small"+i) +'" alt="" style="left: '+ positionX+'px; top: 400px; z-index: 100; position: absolute;"/>');
            positionX = startX*i+10*i+startX;
        }
        i++;    
        if ($("#fadeBanner").attr("data-small"+i) == undefined){
            next = false;
        }
    }
  
    $("img.banner_smallImages").click(function(e){
        var orderNumber = $(this).attr("data-order");
        banner_changeImage(orderNumber);    
        banner_count = 0;   
        currentImage = orderNumber;                                                                         
    });  
  
    function banner_changeImage(orderNumber){
        if (canChange){
            canChange = false;
            $("#topMid").attr("style",'background: url("'+$("#banner_mainImage").attr("src")+'") no-repeat scroll left top #000000');
            $("#banner_mainImage").delay(200).hide(0, function(){
                $("#banner_mainImage").attr("src",$("#fadeBanner").attr("data-image"+orderNumber));
            }).fadeIn(1500, function(){
                canChange = true;
            }); 
        }
    }
  
    /* autoImageChange */
    var banner_count = 9;
    var currentImage = 1;
    function bannerNextImage(){    
        if (banner_count == 19){
            currentImage++;
            skipChange  = false;
            if ($("#fadeBanner").attr("data-small"+currentImage) == undefined){
                if (currentImage == 2){
                    skipChange = true;
                }
                currentImage = 1;
            }
            if (!skipChange){
                banner_changeImage(currentImage);
            }
            banner_count = 9;
        }
        banner_count++;
    }
  
    /* END fadeBanner*/

    repeatEachSecond();     
    core();  
    initAdminImageBox();
    initBanner();
    var preLoadingImages = [
    $("#fadeBanner").attr("data-image1"),
    $("#fadeBanner").attr("data-image2"),
    $("#fadeBanner").attr("data-image3"),
    $("#fadeBanner").attr("data-image4"),
    $("#fadeBanner").attr("data-small1"),
    $("#fadeBanner").attr("data-small2"),
    $("#fadeBanner").attr("data-small3"),
    $("#fadeBanner").attr("data-small4")	
    ];
    preLoadBanner(preLoadingImages);           
    createLightBox();    
    $(window).resize(function() {
        centerScroll();
    });
    centerScroll();
});
function centerScroll(){
    WW = $(window).width();
    if (WW<1400){
        resize = (1400-WW)/2;
        if (resize >= 275){
            resize = 275;
        }
        resize = resize-$(window).scrollLeft();            
        window.scrollBy(resize,0);
    }
}

function createLightBox(){
    $(".clanekRozbalContentImages").each(function(){
        $(this).find("a").lightBox();
    });
}

function preLoadBanner(preLoadingImages){
    $(preLoadingImages).each(function(){
        if (arguments[1] != undefined){
            var image = $('<img />').attr('src', arguments[1]);
        }        
    })
}

function initBanner(){
    $("#banner_mainImage").attr("src",$("#fadeBanner").attr("data-image1"));
}


function initClanky(){
    $(".clanekRozbal").each(function(){
        $(this).find(".clanekRozbalVysouvac").hide();
        nadpis = $(this).find(".clanekRozbalNadpis");       
        vysouvac = $(this).find(".clanekRozbalVysouvac");
        minusPlus = $(this).find(".minusPlus");        
        nadpis.live("click",{
            minusPlus: minusPlus, 
            vysouvac: vysouvac
        },function(event){
            vysouvac = event.data.vysouvac;
            //minusPlus = event.data.minusPlus;
            clanekId = vysouvac.parent().attr("data-clanekName");
            if (vysouvac.is(":hidden")){
                location.hash = "s"+clanekId;
            }else{
                location.hash = "h"+clanekId;
            }
        });
    });
}

function loadAttach(id,fileName,responseJSON){
    loadAtachSimple(responseJSON["idcismap"]);        
}

var loadAtachSimpleCallBack = null;
var numberOfImages = 7;
function loadAtachSimple(idcismap){
    $(".adminImagesBox"+idcismap).html('<img src="customTemplates/Img/Admin_Mode/ico_loading_transparent.GIF" />').load("index.php?ajax=loadAdminImages&idcismap="+idcismap,function(){        
        number = $(this).find(".imageAdminShow").size();
        $(this).find(".atachmentDeleteIco").each(function(){
            $(this).die("click");
            $(this).live("click",function(){
                loadAtachSimpleCallBack = $(this);
                $("#blind").load("index.php?ajax=deleteImage&imageName="+$(this).attr("data-imageName"),function(){
                    loadAtachSimple(loadAtachSimpleCallBack.parent().parent().attr("data-idcismap"));
                });
            });
        })        
        if (number > numberOfImages){
            fileUploader = $("#file-uploader_"+idcismap);            
            fileUploader.hide();            
            fileUploader.parent().find(".maxReached").show();
        }else{
            fileUploader = $("#file-uploader_"+idcismap);            
            fileUploader.show();            
            fileUploader.parent().find(".maxReached").hide();            
        }
    });
}

function initAdminImageBox(){
    $(".adminImagesBox").each(function(){
        loadAtachSimple($(this).attr("data-idcismap"));
    });
}
     
